import api from "@/utils/api";
import {objToFormData} from "@/utils";

export function getCourseApi(pagination: { pageSize: number; pageIndex: number }) {
    return api('/course', {params: pagination})
}

export function addCourseApi({name, img}: { name: string, img: File }) {
    const data = new FormData();
    data.append('name', name);
    data.append('img', img);
    return api.post('/course', data);
}

export function delCourseApi(id: number) {
    return api.delete('/course', {
        params: {id}
    })
}

export function editCourseApi({name, img, id}: { name: string; img: File; id: number }) {
    const data = new FormData();
    data.append('name', name);
    if (img instanceof File) data.append('img', img);
    return api.put('/course', data, {
        params: {
            id
        }
    });
}

export function changeCourseApi(id: number) {
    return api.put('/course/change', {}, {
        params: {
            id
        }
    })
}

export function getVideosApi(courseId: number) {
    return api.get('/video', {params: {id: courseId}})
}

export function uploadVideoApi(data: object) {
    return api.post('/video', objToFormData(data));
}

export function updateVideoApi(data: any, id: number) {
    if (typeof data.video === 'string') delete data.video;
    return api.put('/video', objToFormData(data), {params: {id}});
}
export function delVideoApi(id:number){
    return api.delete('/video',{params:{id}})
}